/*
 * @Author: fhw
 * @Date: 2022-08-02 11:13:27
 * @Description: 消息通知
 */
import { WebSocketGateway, SubscribeMessage, ConnectedSocket, MessageBody } from '@nestjs/websockets'
import { isNil } from 'ramda'
import { SysMessageService } from 'src/sys_message/sys_message.service'
import { ConfigService } from '../config/config.service'
import { SocketMessageService } from './socket-message.service'

@WebSocketGateway()
export class SocketMessageGateway {
  constructor(
    private readonly socketMessageService: SocketMessageService,
    public readonly configService: ConfigService,
    public readonly sysMessageService: SysMessageService,
  ) { }
  // 获取聊天记录
  @SubscribeMessage('unreadChatRecord')
  async unreadChatRecord(@MessageBody() params, @ConnectedSocket() clinet: any) {
    const { userId } = params
    const data = await this.socketMessageService.unreadChatRecord(params)
    if (isNil(this.configService.getChatUser(userId))) return
    clinet.to(this.configService.getChatUser(userId)).emit('unreadChatRecord', data)
  }
  // 获取消息通知
  @SubscribeMessage('messageNotice')
  async messageNotice(@MessageBody() params, @ConnectedSocket() clinet: any) {
    const { userId } = params
    try {
      const { data } = await this.sysMessageService.getSysMessageList({ userId })
      if (isNil(this.configService.getChatUser(userId))) return
      clinet.emit('message', data);
    } catch (error) {
      clinet.emit('chatError', { code: 500, message: error });
    }
  }
  // 设置消息记录
  @SubscribeMessage('setSysMessageStatus')
  async setSysMessageStatus(@MessageBody() params, @ConnectedSocket() clinet: any) {
    const { ids, userId } = params
    try {
      const { success, message } = await this.sysMessageService.setSysMessageStatus(ids)
      if (success) {
        this.messageNotice(userId, clinet)
        return
      }
      clinet.emit('chatError', { code: 500, message: message });
    } catch (error) {
      clinet.emit('chatError', { code: 500, message: error });
    }
  }
  /**
   * 获取好友验证未读记录
   */
  @SubscribeMessage('getFriendsNotice')
  async getFriendsNotice(@MessageBody() params, @ConnectedSocket() clinet: any) {
    const { userId } = params
    try {
      const data = await this.socketMessageService.getUnreadFriendsRecord({ userId })
      if (isNil(this.configService.getChatUser(userId))) return
      clinet.emit('getFriendsNotice', data);
    } catch (error) {
      clinet.emit('chatError', { code: 500, message: error });
    }
  }
  /** 设置好友未读记录 */
  @SubscribeMessage('setFriendsUnreadRecord')
  async setFriendsUnreadRecord(@MessageBody() params, @ConnectedSocket() clinet: any) {
    const { ids, userId } = params
    try {
      const { success, msg } = await this.socketMessageService.setFriendsUnreadRecord(ids)
      if (success) {
        this.getFriendsNotice({userId}, clinet)
        return
      }
      clinet.emit('chatError', { code: 500, message: msg });
    } catch (error) {
      clinet.emit('chatError', { code: 500, message: error });
    }
  }
}
